package cn.web.management.web.servlet.account;

import cn.web.management.domain.UserInform;
import cn.web.management.service.inface.LoginVerify;
import cn.web.management.service.impl.LoginVerifyImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "usernameExistServlet", value = "/usernameExistServlet")
public class usernameExistServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    /**
     * @description: 注册验证Servlet，验证其注册的用户名是否重复，返回字符串true或者false代表该用户名是否可用，true代表可用，false代表不可用
     * @paramName: [req, resp]
     * @paramType: [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @returnType: void
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        LoginVerify loginVerify = new LoginVerifyImpl();
        List<UserInform> list = loginVerify.findUserNamePassword();

        UserInform u;
        boolean flag = true;
        for (int i = 0; i < list.size(); i++) {
            u = list.get(i);
            if(u.getUsername().equals(username)){
                flag = false;//数据库中存在该用户名,不可用
                break;
            }
        }
        resp.getWriter().write(flag+"");
    }
}
